package tongcheng.com.tongcheng.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.List;

import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseViewHolder;
import tongcheng.com.tongcheng.bean.FuwuLeixingBean;

/**
 * Created by P on 2017/7/26.
 */

public class LeiXingAdapter extends RecyclerView.Adapter<LeiXingAdapter.LeiXingViewHolder> {

    public Context mContext;
    public List<FuwuLeixingBean.DataBean.SonListBean> sonList;

    public LeiXingAdapter(Context context, List<FuwuLeixingBean.DataBean.SonListBean> sonList) {
        this.mContext = context;
        this.sonList = sonList;
    }

    @Override
    public LeiXingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_leixing, null);
        LeiXingViewHolder leiXingViewHolder = new LeiXingViewHolder(view);
        return leiXingViewHolder;
    }

    @Override
    public void onBindViewHolder(final LeiXingViewHolder holder, int position) {
        final FuwuLeixingBean.DataBean.SonListBean sonListBean = sonList.get(position);
        holder.leixing_btn.setText(sonListBean.name);

        if (sonListBean.isChoose == false) {
            holder.leixing_btn.setBackgroundResource(R.drawable.bg_xian);
        } else {
            holder.leixing_btn.setBackgroundResource(R.drawable.btn_yellowbg);
        }


        holder.leixing_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (sonListBean.isChoose==true) {
                    holder.leixing_btn.setBackgroundResource(R.drawable.bg_xian);
                    sonListBean.isChoose = false;
                } else if (sonListBean.isChoose==false) {
                    holder.leixing_btn.setBackgroundResource(R.drawable.btn_yellowbg);
                    sonListBean.isChoose = true;
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return sonList.size();
    }

    public class LeiXingViewHolder extends BaseViewHolder{

        public Button leixing_btn;

        public LeiXingViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void init(View itemview) {
            leixing_btn=itemview.findViewById(R.id.leixing_btn);
        }
    }

}
